package com.yjxxt;
import com.yjxxt.dao.DeptDao;
import com.yjxxt.vo.DE;
import com.yjxxt.vo.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DempTest {
    static SqlSessionFactory sqlSessionFactory = null;
    static SqlSession session =null;
    static DeptDao mapper = null;
    static{
        //定义配置文件的路径，这里表示 mybatis-config.xml 位于项目的类路径（classpath）下（通常是 src/main/resources 目录）。
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            //通过 MyBatis 提供的 Resources 工具类，加载配置文件并转换为输入流
            inputStream = Resources.getResourceAsStream(resource);
        }catch (IOException e){
            e.printStackTrace();
        }
        //通过 SqlSessionFactoryBuilder 解析配置文件输入流，创建 SqlSessionFactory（会话工厂），它是创建 SqlSession 的核心工厂类（单例模式，全局通常只需要一个）。
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //创建 SqlSession 实例,可以设置事务自动提交: openSession(true)
        //openSession() 无参或flase: 关闭事务自动提交,需要手动提交session.commit()
        session = sqlSessionFactory.openSession();
        //通过 MyBatis 的 SqlSession 对象获取 DeptDao 接口的代理实现类实例（即 Mapper 代理对象）。
        //后续可通过该 mapper 实例调用接口中定义的方法（如 selectAll()、insertDept() 等），MyBatis 会自动执行对应的 SQL 语句。
        mapper = session.getMapper(DeptDao.class);
    }


    public static void selectMaxDept() {
        Dept dept = mapper.selectMaxDept();
        if (dept != null) {
            System.out.println(dept.getDeptno() + " " + dept.getDname() + " " + dept.getLoc());
        }
    }


    public static void selectDeptAndLeader(){
        List<DE> de = mapper.selectDeptAndLeader();
        if(de!=null){
            for (DE d : de){
                System.out.println(d.getEname() + " " + d.getDname());
            }
        }
        else{
            System.out.println("没有该部门");
        }
    }


    public static void selectByNameAndId(String name, int id) {
        Dept dept = mapper.selectByNameAndId(name, id);
        System.out.println(dept.getDeptno() + " " + dept.getDname() + " " + dept.getLoc());
    }

    public static void selectById(int id) {
        Dept dept = mapper.selectByDeptno(id);
        System.out.println(dept.getDeptno() + " " + dept.getDname() + " " + dept.getLoc());
    }

    public static void TestselectLimit( int pageNum, int pageSize) {
        List<Dept> depts = mapper.selectByPage(pageNum, pageSize);
        for (Dept d : depts){
            System.out.println(d.getDeptno() + " " + d.getDname() + " " + d.getLoc());
        }
    }

    public static void selectAll() {
        List<Dept> depts;
        depts = mapper.selectAll();
        for (Dept d : depts) {
            System.out.println(d.getDeptno() + " " + d.getDname() + " " + d.getLoc());
        }
    }

    public static void Testupdate( String dname, String loc,int deptno) {
        try {
            //删除数据
            mapper.updateDept(dname, loc, deptno);
            session.commit();
        }catch (Exception e) {
            //事务回滚
            session.rollback();
        }
        finally {
            //关闭 SqlSession
            if (session != null) {
                session.close();
            }
        }
    }

    public static void Testdelete(int id) {
        try {
            //删除数据
            mapper.deleteByDeptno(id);
            session.commit();
        }catch (Exception e) {
            //事务回滚
            session.rollback();
        }
        finally {
            //关闭 SqlSession
            if (session != null) {
                session.close();
            }
        }
    }

    public static void Testinsert(int deptno,String dname, String loc) {
        try {
            //增加数据
            System.out.println(mapper.insertDept(new Dept(deptno, dname, loc)));
            session.commit();
        }catch (Exception e) {
            //事务回滚
            session.rollback();
        }
        finally {
            //关闭 SqlSession
            if (session != null) {
                session.close();
            }
        }
    }
}
